import langsData from '../../res/data/langs.json';
import keysData from '../../res/data/keys.json';
import AsyncStorage from '@react-native-community/async-storage';

export const FLAG_LANGUAGE = {
  flage_language: 'language_dao_language',
  flag_key: 'language_dao_key',
};
export default class LanguageDao {
  constructor(flage) {
    this.flage = flage;
  }
  /**
   * 获取语言或标签
   */
  fetch() {
    return new Promise((resolve, reject) => {
      AsyncStorage.getItem(this.flage, (error, result) => {
        if (error) {
          reject(error);
          return;
        }
        if (!result) {
          var data =
            this.flage === FLAG_LANGUAGE.flage_language ? langsData : keysData;
          this.save(data);
          resolve(data);
        } else {
          try {
            resolve(JSON.parse(result));
          } catch (e) {
            reject(e);
          }
        }
      });
    });
  }

  /**
   * 保存语言或标签
   */
  save(objectData) {
    let stringData = JSON.stringify(objectData);
    AsyncStorage.setItem(this.flage, stringData, (error, result) => {});
  }
}
